<?php
define('IN_ROSTER', true);
require_once (dirname(__FILE__) . '/../settings.php');
require_once (ROSTER_LIB . 'update.lib.php');
$update = new update();

class apiguildUpdate
{
	var $guildID;
	var $data=array();
	var $upd;
	var $gname;
	var $gserver;
	var $faction = array(
	0 => 'Alliance',
	1 => 'Horde');
	/*
	$db =& JFactory::getDBO();
	$query = "SELECT * FROM #__guildroster_charinfo ORDER BY name";
	$db->setQuery($query);
	$rows = $db->loadObjectList();
	*/
	
	function getguildid($n,$s)
	{
		global $roster, $update;
		
		$db =& JFactory::getDBO();
		$sqlquery2 = "SELECT guild_id FROM `#__guild_guilds` WHERE `guild_name` = '" . $n . "' AND `server` = '" . $s . "'";
		$result2 = $db->setQuery($sqlquery2);
		$row = $db->loadObjectList();
		///echo '++++'.$row['member_id'].'++++<br>';
		return $row['guild_id'];
	}
	function update_guild($data)
	{
		global $roster, $update, $queue;
		$db =& JFactory::getDBO();
	
		$this->gname = $data['name'];
		$this->gserver =$data['realm'];
		$this->guildID = $this->getguildid($data['name'],$data['realm']);

		$update->reset_values();
		$update->add_value('guild_name', $data['name'] );
		$update->add_value('update_time', time() );
		$update->add_value('region', 'us' );
		$update->add_value('server', $data['realm'] );
		$update->add_value('faction', $data['side'] );
		$update->add_value('factionEn', $this->faction[$data['side']] );
		
		$update->add_value('guild_num_members', count($data['members']) );
		$update->add_value('guild_level', $data['level'] );
		
		if( isset($this->guildID) )
		{
			$querystr = "UPDATE `#__guild_guilds` SET " . $update->assignstr . " WHERE `guild_id` = '".$this->guildID."';";
			$this->upd = true;
		}
		else
		{
			$querystr = "INSERT INTO `#__guild_guilds` SET " . $update->assignstr . ";";
		}
		//echo $querystr.'<br>';
		$ret = $db->setQuery($querystr);
		if( !isset($this->guildID) )
		{
			$this->guildID = $this->getguildid($data['name'],$data['realm']);
		}
		//echo '<br>--'.$this->guildID.'--<br>';
		//return true;
		return $data = array('name'=>$data['name'],'server'=>$data['realm'],'id'=>$this->guildID,'data'=>array($data));
	}
	
	
}

?>